﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using vmlModel.Entities;
using System.Windows.Controls;

namespace XAMLFactory
{
    public class StrokeLineStyleBuilder
    {
        public Brush vmlRecLineStyle(vmlShapeBase SimpleShape)
        {
            VisualBrush lineStyle = new VisualBrush();
            if (SimpleShape.Stroke == null) return new SolidColorBrush(SimpleShape.StrokeColor);
            if (!SimpleShape.Stroke.LiveAtrrs.Contains("linestyle") || SimpleShape.Stroke.LineStyle == "solid") return new SolidColorBrush(SimpleShape.StrokeColor);
            double StrokeWeight = (SimpleShape.Stroke.LiveAtrrs.Contains("weight"))?SimpleShape.Stroke.Weight : SimpleShape.StrokeWeight; 
            // Container
            Canvas LineStyleContainer = new Canvas();
            // first Border
            Border b1 = new Border();
            b1.Width = SimpleShape.Width;
            b1.Height = SimpleShape.Height;
            b1.BorderBrush = new SolidColorBrush(SimpleShape.StrokeColor);
            b1.Background = new SolidColorBrush(Colors.Transparent);
            // seconed Border
            Border b2 = new Border();
            b2.BorderBrush = new SolidColorBrush(Colors.Transparent);
            b2.Background = new SolidColorBrush(Colors.Transparent);
            // seconed Border
            Border b3 = new Border();
            b3.BorderBrush = new SolidColorBrush(SimpleShape.StrokeColor);
            b3.Background = new SolidColorBrush(Colors.Transparent);

            if (SimpleShape.GetType() == typeof(vmlRoundrect))
            {
                b1.CornerRadius = new System.Windows.CornerRadius((SimpleShape as vmlRoundrect).CornerRadius);
            }
            if (SimpleShape.GetType() == typeof(vmlOval))
            {
                b1.CornerRadius = new System.Windows.CornerRadius(Math.Min(SimpleShape.Width,SimpleShape.Height));
            }

            b2.CornerRadius = b3.CornerRadius = b1.CornerRadius;

            //visual brushes part
            // "thinthin", create a rectangle with the outline (1:1:1)
            //"thinthick", create a rectangle with the outline (1:1:2)
            //"thickthin", create a rectangle with the outline (2:1:1)
            //"thickbetweenthin",create a rectangle with the outline (1:1:2:1:1)
            switch (SimpleShape.Stroke.LineStyle.ToLower())
            {
                case "thinthin":
                   b1.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 3);
                   b2.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 3);
                   b3.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 3);
                    break;
                case "thinthick":
                   b1.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 4);
                   b2.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 4);
                   b3.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 2);
                    break;
                case "thickthin":
                   b1.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 2);
                   b2.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 4);
                   b3.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 4);
                    break;
                case "thickbetweenthin":
                   b1.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 6);
                   b2.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 6);
                   b3.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 3);
                    // extra borders
                    Border b4 = new Border();
                    b4.BorderBrush = new SolidColorBrush(Colors.Transparent);
                    b4.Background = new SolidColorBrush(Colors.Transparent);
                    b4.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 6);
                    Border b5 = new Border();
                    b5.BorderBrush = new SolidColorBrush(SimpleShape.StrokeColor);
                    b5.Background = new SolidColorBrush(Colors.Transparent);
                    b5.BorderThickness = new System.Windows.Thickness(StrokeWeight * 1 / 6);
                    b4.CornerRadius = b5.CornerRadius = b1.CornerRadius; 
                    // adding them
                    b4.Child = b5;
                    b3.Child = b4;
                    break;
                default:
                    break;
            }
            b2.Child = b3;
            b1.Child = b2;
            //
           LineStyleContainer.Children.Add(b1);
            lineStyle.Visual = LineStyleContainer ;
            return lineStyle;
        }
    }
}
